package com.js.myapp.application;

// Created by JS on 2021/1/20.

import android.app.Application;
import android.content.Context;

import com.js.myapp.common.db.DBService;
import com.js.myapp.common.db.MySQLiteOpenHelper;
import com.js.myapp.gen.DaoMaster;
import com.js.myapp.gen.DaoSession;

public class MyApplication extends Application {

    private static MyApplication sInstance;

    private static DaoMaster sDaoMaster;
    private static DaoSession sDaoSession;

    @Override
    public void onCreate() {
        super.onCreate();
        if (sInstance == null) sInstance = this;
    }

    public static MyApplication getInstance() {
        return sInstance;
    }

    /**
     *  获取 DaoMaster
     * @param context 上下文
     * @return DaoMaster
     */
    public static DaoMaster getDaoMaster(Context context) {
        if (sDaoMaster == null) {
            MySQLiteOpenHelper helper = new MySQLiteOpenHelper(context, DBService.DB_NAME, null);
            sDaoMaster = new DaoMaster(helper.getWritableDatabase());
        }
        return sDaoMaster;
    }

    /**
     *  获取 DaoSession
     * @param context 上下文
     * @return DaoSession
     */
    public static DaoSession getDaoSession(Context context) {
        if (sDaoSession == null) {
            if (sDaoMaster == null)
                sDaoMaster = getDaoMaster(context);
            sDaoSession = sDaoMaster.newSession();
        }
        return sDaoSession;
    }
}
